package com.lyw.home

import android.content.Context
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.lu.library.base.BaseActivity
import com.lyw.home.bean.DetailIntro
import com.lyw.home.databinding.ActivityAuthorDetailBinding
import com.lyw.home.databinding.ActivityPoetyDetailBinding
import com.lyw.home.databinding.ItemAuthorDetailBinding
import com.lyw.home.vm.AuthorDetailVm
import com.lyw.home.vm.PoetryDetailVm
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.util.concurrent.Executors

@Route(path = HomeRouterPaths.HOME_AUTHOR_DETAIL)
@AndroidEntryPoint
class AuthorDetailActivity :BaseActivity<ActivityAuthorDetailBinding>(){


    @JvmField
    @Autowired
    var id:Int=0

    private val authorDetailVm by viewModels<AuthorDetailVm>()

    private val detailAdapter = object:BaseQuickAdapter<DetailIntro,DataBindingHolder<ItemAuthorDetailBinding>>(){
        override fun onBindViewHolder(
            holder: DataBindingHolder<ItemAuthorDetailBinding>,
            position: Int,
            item: DetailIntro?
        ) {
            LogUtils.d(item)
            holder.binding.tvDetailTitle.text = item?.title?:""
            holder.binding.tvDetailContent.text = item?.content?:""
        }

        override fun onCreateViewHolder(
            context: Context,
            parent: ViewGroup,
            viewType: Int
        ): DataBindingHolder<ItemAuthorDetailBinding> {
           return DataBindingHolder(R.layout.item_author_detail,parent)
        }

    }
    override fun initData() {
        Executors.newSingleThreadExecutor()
        lifecycleScope.launch {
            authorDetailVm.authorDetail.collect{
                binding.tvAuthorName.text = it.name
                binding.tvAuthorDetail.text = it.simpleIntro
                binding.tvAuthorDetail2.text = it.detailIntro
                binding.toolbar.title = it.name
            }
        }
        lifecycleScope.launch {
            authorDetailVm.detailList.collect{
                LogUtils.d("list:${it.size}")
                detailAdapter.addAll(it)
//                detailAdapter.notifyDataSetChanged()
            }
        }
        authorDetailVm.initData(id)

    }

    override fun initViews() {
        super.initViews()
        binding.rvDetail.layoutManager = LinearLayoutManager(this)
        binding.rvDetail.adapter = detailAdapter
//        binding.nestedScrollView.updatePadding(bottom = BarUtils.getNavBarHeight())
    }

    override fun layoutResId(): Int = R.layout.activity_author_detail
}